
import request from '@/base/request'
import { ElMessage } from 'element-plus'
import {ref} from 'vue'
import Problem from '@/types/problem'

export default function(){

    interface AddProblem{
        id?:number,
        type?:number,
        problemDescribe?:string,
        problemCode?:string,
        analysis?:string,
        difficulty?:number,
        answer?:Array<String>,
        knowledgeList?:Array<number>,
        optionDescribe?:Array<String>,
        codeLanguage?:string,
        visible?:boolean,
        aiReply?:string
    }
    interface SkipOrFail{
        rowIndex:number,
        reason:string,
    }

    interface UploadProblemDetail{
        totalProblemNumber?:number,
        skipOrFailVOList?:Array<SkipOrFail>
        successProblemIds?:Array<Problem>
    }

    interface AIProblemParm{
        typeString:string,
        description:string
    }

    const aiProblemParm=ref<AIProblemParm>({
        typeString:"单选",
        description:""
    })

    const uploadProblemDetail=ref<UploadProblemDetail>()



    const repeatSkip=ref(true)

    const addProblem=ref<AddProblem>({
        visible:true,
        difficulty:1,
        type:1,
        optionDescribe:[""],
        answer:[""],
        codeLanguage: 'cpp'
    })


    function init(){
        addProblem.value={
            visible:true,
            difficulty:1,
            type:1,
            optionDescribe:[""],
            answer:[""],
            codeLanguage: 'cpp'
        }
    }

    async function addProblemAPI() {
        const response=(await request.post("/problem/admin/addOrUpdateProblem",addProblem.value)).data
        if(response.code===200){
            ElMessage.success("成功")
            init()
        }else{
            ElMessage.error(`失败${response.msg}`)
        }
    }

    async function deleteByid(id:number) {
        const response=(await request.delete(`/problem/admin/delById/${id}`)).data
        if(response.code===200){
            ElMessage.success("删除成功")
        }else{
            ElMessage.error(`删除失败${response.msg}`)
        }
    }

    async function handleBeforeUpload(file) {
        const formData=new FormData();
        formData.append('file',file);
        formData.append('repeatSkip',repeatSkip.value);
        const response=(await request.post('/problem/admin/uploadProblem',formData,{
            headers:{
                'Content-Type': 'multipart/form-data'
            }
        })).data
        if(response.code===200){
            uploadProblemDetail.value=response.data
            
        }else{
            ElMessage.error(`上传失败：${response.msg}`)
        }
        return false;
    }


    async function getAIProblem() {
        const response=(await request.post(`/judge/admin/chatGLM/getAIProblem`,aiProblemParm.value)).data
        if(response.code===200){
            addProblem.value=response.data;
            if(!response.data.transform){
                ElMessage.error("自动解析成题目失败，请根据AI回复手动编辑题目，或者重新获取")
            }
        }else{
            ElMessage.error(`发送错误，${response.msg}`)
        }
    }


    return {addProblem,repeatSkip,uploadProblemDetail,aiProblemParm,getAIProblem,handleBeforeUpload,deleteByid,init,addProblemAPI}
}